package com.xinbochuang.machine.framework.security.custom;

import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 身份证号认证失败
 *
 * @author 黄晓鹏
 * @date 2020-09-14 12:06
 */
@Component
public class FailAuthenticationHandler extends SimpleUrlAuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException e) throws IOException, ServletException {
        int code = HttpStatus.HTTP_UNAUTHORIZED;
        ServletUtils.renderString(response, JSONUtil.toJsonStr(AjaxResult.error(code, e.getMessage())));
    }
}
